package com.sevennotes.classtable.ui.pages.components

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Card
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.sevennotes.classtable.model.ClassRecordMonthStatis
import com.sevennotes.classtable.utils.FakeDataUtil

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun BarChartCard(
  modifier: Modifier = Modifier,
  data: List<ClassRecordMonthStatis> = emptyList(),
  year: String = "",
  maxValue: Int? = null
) {
  Card(
    modifier = modifier.fillMaxWidth()
  ) {
    Column {
      val yearTotal by rememberUpdatedState(newValue = data.sumOf { it.total })
      Row(
        modifier = Modifier.padding(horizontal = 12.dp, vertical = 8.dp),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.SpaceEvenly
      ) {
        Text(
          text = "${year}年"
        )
        Spacer(modifier = Modifier.weight(1f))
        Text(
          text = "总数：$yearTotal"
        )
      }
      BarChart(
        data = data,
        chartHeight = 200.dp,
        maxValue = maxValue
      )
    }
  }
}



@Preview(showBackground = true)
@Composable
fun BarChartCardPreview() {
  BarChartCard(
    year = "2023",
    data = FakeDataUtil.getClassRecordStatisData()
  )
}